package com.example.arithmeticleetcode.learnArithmetic.twonodetree;

import com.example.arithmeticleetcode.learnArithmetic.twonodetree.util.BinaryTrees;

import java.util.Comparator;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-08-25 18:02
 **/
public class BinarySearchTreeTest {

    private static class PersonComparator implements Comparator<Person> {


        @Override
        public int compare(Person e1, Person e2) {
            return e1.getAge() - e2.getAge();
        }
    }

    private static class PersonComparator2 implements Comparator<Person> {


        @Override
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }

    static void test1() {
        Integer[] arr = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3};
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.add(arr[i]);
        }
        BinaryTrees.print(binarySearchTree);
    }

    static void test2() {

        Integer[] arr = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3};

        BinarySearchTree<Person> personBinarySearchTree = new BinarySearchTree<>();

        for (int i = 0; i < arr.length; i++) {
            personBinarySearchTree.add(new Person(arr[i], "" + i));
        }

        BinaryTrees.print(personBinarySearchTree);
    }

    static void test3() {

        Integer[] arr = new Integer[]{7, 4, 9, 2, 5, 8, 11, 3};

        BinarySearchTree<Person> personBinarySearchTree = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });

        for (int i = 0; i < arr.length; i++) {
            personBinarySearchTree.add(new Person(arr[i], "" + i));
        }

        BinaryTrees.print(personBinarySearchTree);
    }

    static void test03() {
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();

        for (int i = 0; i < 30; i++) {
            binarySearchTree.add((int) (Math.random() * 100));
        }

        BinaryTrees.print(binarySearchTree);

    }

    static void test5() {
        BinarySearchTree<Person> binarySearchTree = new BinarySearchTree<>();
        binarySearchTree.add(new Person(10, "jack"));
        binarySearchTree.add(new Person(12, "rose"));
        binarySearchTree.add(new Person(6, "jim"));
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        System.out.println();
        System.out.println();
        binarySearchTree.add(new Person(10, "michael"));
        BinaryTrees.print(binarySearchTree);
    }

    static void test6() {
        Integer[] arr = new Integer[]{7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.add(arr[i]);
        }
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        //前序遍历 7 4 2 1 3 5 9 8 11 10 12
//        binarySearchTree.preOrderTraversal();
        //中序遍历 1 2 3 4 5 6 7 8 9 10 11 12
//        binarySearchTree.inOrderTraversal();
        //后序遍历 1 3 2 5 4  8 10 12 11 9 7
//        binarySearchTree.postOrderTraversal();

        //层序遍历 7 4 9 2 5 8 11 1 3 10  12
//        binarySearchTree.levelOrderTraversal();
//        binarySearchTree.levelOrder(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public void visit(Integer element) {
//                System.out.println("_" + element + "_");
//            }
//        });
        binarySearchTree.inOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_");
                return true;
            }
        });
    }

    static void test07() {
        Integer[] arr = new Integer[]{7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.add(arr[i]);
        }
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        binarySearchTree.inOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_");
                return element == 3 ? true : false;
            }
        });

        System.out.println();

        binarySearchTree.preOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_");

                return element == 2 ? true : false;
            }
        });

        System.out.println();
        binarySearchTree.postOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_");
                return element == 2 ? true : false;
            }
        });

        System.out.println();
        binarySearchTree.levelOrder(new BinarySearchTree.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.print(element + "_");
                return element == 9 ? true : false;
            }
        });

    }

    static void test08() {
        Integer[] arr = new Integer[]{7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.add(arr[i]);
        }
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        String s = binarySearchTree.toString();
        System.out.println(s);

        System.out.println("height" + binarySearchTree.height());

    }

    static void test09() {
        BinarySearchTree<Object> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < 2; i++) {
            binarySearchTree.add((int)(Math.random() * 100));
        }
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        System.out.println("height" + binarySearchTree.height());
        System.out.println("完全二叉树" + binarySearchTree.isComplete());
    }

    static void test10() {
        Integer[] arr = new Integer[]{7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12};
        BinarySearchTree<Integer> binarySearchTree = new BinarySearchTree<>();
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.add(arr[i]);
        }
        BinaryTrees.print(binarySearchTree);
        System.out.println();
        binarySearchTree.remove(11);
        BinaryTrees.print(binarySearchTree);
        System.out.println();

        System.out.println(binarySearchTree.contains(9));


    }

    public static void main(String[] args) throws Exception {
        test10();

//        String srcString = "我们是中国人";
//        String utf2GbkString = new String(srcString.getBytes("UTF-8"),"GBK");
//        System.out.println("UTF-8转换成GBK："+utf2GbkString);
//        String utf2Gbk2UtfString = new String(utf2GbkString.getBytes("GBK"),"UTF-8");
//        System.out.println("UTF-8转换成GBK再转成UTF-8："+utf2Gbk2UtfString);
//
//        System.out.println("=========================================");
//
//        String srcString1 = "我们是中国人";
//        String gbk2UtfString = new String(srcString1.getBytes("GBK"), "UTF-8");
//        System.out.println("GBK转换成UTF-8：" + gbk2UtfString);
//        String gbk2Utf2GbkString = new String(gbk2UtfString.getBytes("UTF-8"), "GBK");
//        System.out.println("GBK转换成UTF-8再转成GBK：" + gbk2Utf2GbkString);


    }
}
